/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import Models.PostMortem;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author HUY
 */
public class ManagingPosMortemDAO {

    public static  List<PostMortem> loadAllPostMortem() {
        List<PostMortem> list = new ArrayList<PostMortem>();
        Connection con = DBUtility.openConnection();
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {

            stm = con.prepareCall("select * from PostMortem");
            rs = stm.executeQuery();
            while (rs.next()) {
                PostMortem postMortem = new PostMortem();
                postMortem.setDoctorName(rs.getString("MortalityName"));
                postMortem.setMortalityTime(rs.getDate("MortalityTime"));

                postMortem.setCaseID(rs.getInt("CaseID"));
                postMortem.setCheckInDate(rs.getDate("CheckInDate"));
                postMortem.setCauseOfDeath(rs.getString("CaseType"));
                postMortem.setDoctorName(rs.getString("DoctorName"));

                list.add(postMortem);

            }

        } catch (SQLException ex) {
            Logger.getLogger(CaseHistoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {

            DBUtility.closeResultSet(rs);
            DBUtility.closeStatement(stm);
            DBUtility.closeConnection(con);
        }
        return list;
    }

    public static  boolean addNewPosMortem(PostMortem postMortem) {
        //SimpleDateFormat df = new SimpleDateFormat("dd/mm/yy");
        boolean result = false;
        Connection con = DBUtility.openConnection();
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            stm = con.prepareStatement("insert PostMortem(MortalityName,MortalityTime,CaseID,CheckInDate,CauseOfDeath,DoctorName) values  (?,?,?,?,?,?)");
            stm.setString(1, postMortem.getMortalityName().trim());
            java.sql.Date _sqlMortalityTime = new java.sql.Date(postMortem.getMortalityTime().getTime());
            stm.setDate(2, _sqlMortalityTime);
            stm.setInt(3, postMortem.getCaseID());
            java.sql.Date _sqlCheckInDate = new java.sql.Date(postMortem.getCheckInDate().getTime());
            stm.setDate(4, _sqlCheckInDate);

            stm.setString(5, postMortem.getCauseOfDeath().trim());
            stm.setString(6, postMortem.getDoctorName().trim());

            if (stm.executeUpdate() > 0) {
                result = true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(CaseHistoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DBUtility.closeResultSet(rs);
            DBUtility.closeStatement(stm);
            DBUtility.closeConnection(con);
        }

        return result;
    }
}
